/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-1-4 21:53:17
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#include "conclient.h"

ConClient::ConClient(net_reactor_t* pReactorEngine)
{
    m_pReactorEngine = pReactorEngine;
	m_cli_fd = -1;
}

ConClient::~ConClient()
{
    m_pReactorEngine->close_fd(m_cli_fd);
}

int ConClient::startConnect(const char* ip, int port)
{
	if(m_pReactorEngine)
		m_cli_fd = m_pReactorEngine->open_tcp_client(ip, port);
	return m_cli_fd;
}

int ConClient::send_data(const char* data, int len)
{
    if(m_pReactorEngine)
        m_pReactorEngine->send(m_cli_fd, data, len);
    return 0;
}

//callback
CallbackHandler::CallbackHandler(net_reactor_t* pReactorEngine)
{
    m_pReactorEngine = pReactorEngine;
}
int CallbackHandler::on_connect(SOCK_FD listen_fd, SOCK_FD cli_fd, const char* remote_ip, int remote_port)
{
	printf("on_Connect(%d, %s, %d)\n", cli_fd, remote_ip, remote_port);
	return 0;
}

int CallbackHandler::on_data(SOCK_FD fd, const char* data, int len, const char* remote_ip, int remote_port)
{
    //printf("CPPClient::on_data len=[%d]\n", len);
    if(m_pReactorEngine)
        m_pReactorEngine->send(fd, data, len);
	return 0;
}

int CallbackHandler::on_send_ack(SOCK_FD fd, int len)
{
	return 0;
}

int CallbackHandler::on_close(SOCK_FD fd, int error)
{
	printf("on_close(%d, %d)\n", fd, error);
	return 0;
}



